package com.facebook.games.fa.business
{
	import com.facebook.games.fa.events.ChooseTeamEvent;
	import com.facebook.games.fa.services.TeamService;
	import com.facebook.games.fa.util.Responder;
	
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.logging.Log;
	import mx.rpc.IResponder;
	
	[Event(name="teamsLoaded", type="com.facebook.games.fa.events.ChooseTeamEvent")]
	[Event(name="loadError", type="com.facebook.games.fa.events.ChooseTeamEvent")]
	
	/**
	 * Business Object para escolha de times
	 * @author Diego Maehler <dmaehler@gmail.com>
	 */
	public class ChooseTeamBO extends EventDispatcher
	{
		
		public var teams : ArrayCollection = new ArrayCollection();
		
		/**
		 * Carrega as equipes
		 */
		public function loadTeams () : void
		{
			new TeamService(new Responder(this.loadTeamsResult, this.loadTeamsFault)).loadTeams();
		}
		
		/**
		 * Result
		 */
		protected function loadTeamsResult (event : Object) : void
		{
			this.teams = ArrayCollection(event.result);
			this.dispatchEvent(new ChooseTeamEvent(ChooseTeamEvent.TEAMS_LOADED));
		}
		
		/**
		 * Fault
		 */
		protected function loadTeamsFault (event : Object) : void
		{
			this.dispatchEvent(new ChooseTeamEvent(ChooseTeamEvent.LOAD_ERROR));
			Log.getLogger("chooseTeam").error(event.fault.message);
		}
		
	}
	
}

